# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html



# useful for handling different item types with a single interface


# 如果想使用管道，必须去setting去开启
# item就是yeid的对象
class DangdangScrapyPipeline:
    def open_spider(self, spider):
        print("开启管道")
        self.fp = open("./dangdangwang.json", "w", encoding="utf-8")


    def process_item(self, item, spider):
        self.fp.write(str(item))
        return item

    def close_spider(self, spider):
        print("关闭管道")
        self.fp.close()


import urllib


class DangdangScrapyDownloadPipeline:
    def process_item(self, item, spider):
        print("下载图片.......")
        src = item.get('img_url')
        title = item.get('title')
        file_name = './book/' + title + '.jpg'
        urllib.request.urlretrieve(src, file_name)

        return item
